"""
事件循环

并发
"""

import time
import asyncio


async def get_html(url):
    print("start get url")
    # await的用法：await Awaitable
    await asyncio.sleep(2)
    print("end get url")


def use_asyncio():
    start_time = time.time()
    # 事件循环
    loop = asyncio.get_event_loop()

    # 并发的方式
    all_task = [get_html("http://www.imooc.com") for i in range(3)]
    loop.run_until_complete(asyncio.wait(all_task))

    print("耗时：", time.time() - start_time)
    # 输出结果：
    """
    start get url
    start get url
    start get url
    end get url
    end get url
    end get url
    耗时： 2.015568971633911
    """


if __name__ == '__main__':
    use_asyncio()
